import com.kaifamiao.message.Message;
import com.kaifamiao.message.MessageDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/PagingServlet")
public class PagingServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        resp.setIntHeader("Refresh", 1);
        PrintWriter out = resp.getWriter();
        // 每页3条记录
        int pageSize = 5;
        String spage = req.getParameter("page");
        if (spage == null || spage == "") {
            spage = "1";
        }
        int currentPage = Integer.parseInt(spage);
        int totalPage = MessageDao.totalPages(pageSize);

        List<Message> list = MessageDao.getRecords(currentPage, pageSize);

        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");





        String title = "Servlet版本留言板" + "当前时间为: " + ft.format(new Date());
        String docType = "<!DOCTYPE html>\n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1><hr>\n");

        out.println("<center>");
        out.print("<p>当前第: " + spage + "/" + totalPage + " 页</p>");
        out.println("<table border=2>");
        out.println("<tr>");
        out.println("<td>ID</td>");
        out.println("<td>留言人</td>");
        out.println("<td>主題</td>");
        out.println("<td>内容</td>");
        out.println("<td>留言时间</td>");
        out.println("</tr>");

        for (Message e : list) {
            out.print("<tr>");
            out.println("<td>");

            Date d = new Date();
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");

            String name =e.getCreatetime();
            String sname = name.substring(0,10);

            boolean bp = sm.format(d).equals(sname);
            if(bp){
                out.println("<img src='image/new.png'  width='80px' height='40px'>");
            }
            out.println(e.getId() + "</td>");
            out.println("<td>" + e.getUser());
            out.println("<img src='image/f9.ico' width='40px' height='40px'>");
            out.println("</td>");
            if(bp){
                out.println("<td><B>" + e.getTitle() + "</B></td>");
                out.println("<td><B>" + e.getContext() + "</B></td>");
                out.println("<td><B>" + e.getCreatetime()+ "</B></td>");
                out.println("</tr>");
            }else{
                out.println("<td>" + e.getTitle() + "</td>");
                out.println("<td>" + e.getContext() + "</td>");
                out.println("<td>" + e.getCreatetime()+ "</td>");
                out.println("</tr>");
            }

        }
        out.print("</table>");
        out.println("<hr>");
        out.print("<a href='PagingServlet?page=1'>首页</a> ");
        for (int i = 1; i <= totalPage; i++) {
            out.print("<a href='PagingServlet?page=" + i + "'>" + i + "</a> ");
        }
        out.print("<a href='PagingServlet?page=" + totalPage + "'>最后一页</a> ");

        out.println("</center>");
        out.close();
    }

}